﻿using Microsoft.Extensions.Configuration;

namespace Common.YamlConfiguration;

public class AppConfigProvider : ConfigurationProvider
{
    private readonly AppConfigSource _source;

    public AppConfigProvider(AppConfigSource source)
    {
        _source = source;
    }

    public override bool TryGet(string key, out string? value)
    {
        if (base.TryGet(key, out value))
        {
            return true;
        }

        value = "";
        return false;
    }


    public override void Load()
    {
        var file = Path.Combine(AppContext.BaseDirectory, _source.Path);
        using var fs = File.Open(file, FileMode.Open);
        var parser = new YamlConfigurationFileParser();
        var dictionary = parser.Parse(fs);
        Data = dictionary;
    }
}